﻿(function () {
    $(function () {

        var _$dataTable = $('#DataTableItemCategory');
        var _dataService = abp.services.app.itemCategory;

        var _dataServiceItemDetail = abp.services.app.itemDetail;
        //对应AppPermissions里面的Pages_BasicDataModule_CodeGroups_Create等变量,如果这个地方不定义，列表里面不会出现按钮
        var _permissions = {
            create: abp.auth.hasPermission('Pages.PSSModule.ItemCategories.Create'),
            edit: abp.auth.hasPermission('Pages.PSSModule.ItemCategories.Edit'),
            viewItemDetail: abp.auth.hasPermission('Pages.PSSModule.ItemCategories.ViewItemDetail'),
            'delete': abp.auth.hasPermission('Pages.PSSModule.ItemCategories.Delete'),
            editItemDetail: abp.auth.hasPermission('Pages.PSSModule.ItemDetails.Edit')
        };
        //新增/编辑物品分类窗体定义
        var _createOrEditModal = new app.ModalManager({
            viewUrl: abp.appPath + 'PSSModule/ItemCategories/CreateOrEditModal',
            scriptUrl: abp.appPath + 'Areas/PSSModule/Views/ItemCategories/_CreateOrEditModal.js',
            modalClass: 'CreateOrEditModal'//类名跟_CreateOrEditModal.js文件里面的类名app.modals.CreateOrEditModal一致，就能调用模态窗口了
        });
        //新增/编辑物品明细窗体定义
        var _createOrEditModalItemDetail = new app.ModalManager({
            viewUrl: abp.appPath + 'PSSModule/ItemDetails/CreateOrEditModal',
            scriptUrl: abp.appPath + 'Areas/PSSModule/Views/ItemDetails/_CreateOrEditModal.js',
            modalClass: 'CreateOrEditModal'//类名跟_CreateOrEditModal.js文件里面的类名app.modals.CreateOrEditModal一致，就能调用模态窗口了
        });

        //物品分类表格定义，包括样式，触发事件等。
        _$dataTable.jtable({

            //title: app.localize('Pages_BasicDataModule_Classrooms'),
            //paging: true,
            sorting: true,
            multiSorting: true,

            actions: {
                listAction: {
                    method: _dataService.getObjects
                }
            },

            fields: {
                id: {
                    key: true,
                    list: false
                },
                actions: {
                    title: app.localize('Actions'),
                    width: '20%',
                    sorting: false,
                    list: _permissions.edit || _permissions.delete,
                    display: function (data) {
                        var $span = $('<span></span>');

                        if (_permissions.edit) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Edit') + '"><i class="fa fa-edit"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    _createOrEditModal.open({ id: data.record.id });
                                });
                        }

                        if (_permissions.viewItemDetail) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Pages_PSSModule_ItemCategories_ViewItemDetail') + '"><i class="fa fa-th-list"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    //点击刷新代码列表
                                    //abp.ajax({
                                    //    data: JSON.stringify({
                                    //        codeGroupId:data.record.id
                                    //    })
                                    //});
                                    //alert(data.record.codeGroupName);
                                    dataTableItemDetail.load(data.record.id, data.record.itemCategoryName);
                                });
                        }

                        return $span;
                    }
                },
                //网格里面字段必须驼峰写法
                itemCategoryName: {
                    title: app.localize('Pages_PSSModule_ItemCategories_GridColumn_CategoryName'),
                    width: '30%'
                },

            }

        });//表格定义结束

        //物品明细表格
        var dataTableItemDetail = {
            $table: $('#DataTableItemDetail'),
            $emptyInfo: $('#ItemListEmptyInfo'),
            $selectedItemCategoryTitle: $('#SelectedItemCategoryTitle'),
            $selectedItemCategoryID: $('#SelectedItemCategoryID'),
            $addItemButton: $('#AddItemButton'),

            showTable: function (selectItemCategoryName, id) {
                dataTableItemDetail.$emptyInfo.hide();

                dataTableItemDetail.$table.show();
                dataTableItemDetail.$addItemButton.show();
                dataTableItemDetail.$selectedItemCategoryTitle.text(': ' + selectItemCategoryName).show();
                dataTableItemDetail.$selectedItemCategoryID.text(id).hide();

            },
            hideTable: function () {
                dataTableItemDetail.$table.hide();
                dataTableItemDetail.$addItemButton.hide();
                dataTableItemDetail.$selectedItemCategoryTitle.hide();
                dataTableItemDetail.$selectedItemCategoryID.hide();
                dataTableItemDetail.$emptyInfo.show();
            },
            load: function (selectItemCategoryId, selectItemCategoryName) {
                //如果没有点击子代码按钮，则执行hideTable函数

                if (selectItemCategoryId == null) {
                    dataTableItemDetail.hideTable();
                    return;
                }

                dataTableItemDetail.showTable(selectItemCategoryName, selectItemCategoryId);
                //加载数据方法
                dataTableItemDetail.$table.jtable('load', {
                    filter: selectItemCategoryId //过滤条件

                });
            },
            init: function () {
                dataTableItemDetail.$table.jtable({
                    //title: app.localize('Users'),
                    paging: true,
                    multiSorting: true,
                    actions: {
                        listAction: {
                            method: _dataServiceItemDetail.getObjects
                        }
                    },

                    fields: {
                        actions: {
                            title: app.localize('Actions'),
                            width: '15%',
                            list: _permissions.manageMembers,
                            display: function (data) {
                                var $span = $('<span></span>');

                                if (_permissions.editItemDetail) {
                                    $('<button class="btn btn-default btn-xs" title="' + app.localize('Edit') + '"><i class="fa fa-edit"></i></button>')
                                        .appendTo($span)
                                        .click(function () {
                                            //alert($('#SelectedCodeGroupID').text())
                                            _createOrEditModalItemDetail.open({ id: data.record.id, itemCategoryId: $('#SelectedItemCategoryID').text() });
                                        });
                                }

                                if (_permissions.delete) {
                                    $('<button class="btn btn-default btn-xs" title="' + app.localize('Delete') + '"><i class="fa fa-trash-o"></i></button>')
                                        .appendTo($span)
                                        .click(function () {
                                            deleteObjectItemDetail(data.record);
                                        });
                                }

                                return $span;
                            }

                        },
                        itemName: {
                            title: app.localize('Pages_PSSModule_ItemDetails_GridColumn_ItemName'),
                            width: '25%'
                        },
                        itemCode: {
                            title: app.localize('Pages_PSSModule_ItemDetails_GridColumn_ItemCode'),
                            width: '15%'
                        },
                        specification: {
                            title: app.localize('Pages_PSSModule_ItemDetails_GridColumn_Specification'),
                            width: '15%'
                        },
                        purchasePrice: {
                            title: app.localize('Pages_PSSModule_ItemDetails_GridColumn_PurchasePrice'),
                            width: '10%'
                        },
                        salePrice: {
                            title: app.localize('Pages_PSSModule_ItemDetails_GridColumn_SalePrice'),
                            width: '10%'
                        },
                        units: {
                            title: app.localize('Pages_PSSModule_ItemDetails_GridColumn_Units'),
                            width: '10%'
                        }
                    }
                });

                

                dataTableItemDetail.hideTable();
            }
        }
        //加载物品分类数据方法
        function getObjects(reload) {
            if (reload) {
                _$dataTable.jtable('reload');
            } else {
                _$dataTable.jtable('load', {
                    filter: '' //如果没有查询条件输入框，此处置为空
                });
            }
        }
        //删除物品信息
        function deleteObjectItemDetail(o) {
            abp.message.confirm(
                app.localize('Com_Msg_Delete_Warning', o.itemName),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _dataServiceItemDetail.deleteObject({
                            id: o.id
                        }).done(function () {
                            dataTableItemDetail.load($('#SelectedItemCategoryID').text(), $('#SelectedItemCategoryTitle').text().replace(":", ""));
                            abp.notify.success(app.localize('SuccessfullyDeleted'));
                        });
                    }
                }
            );
        }
        
        //打开新增/编辑代码组页面
        $('#AddCodeGroupButton').click(function (e) {
            e.preventDefault();
            _createOrEditModal.open();
        });

        //打开新增/编辑产品明细页面
        $('#AddItemButton').click(function (e) {
            e.preventDefault();
            //alert($('#SelectedItemCategoryID').text());
            //页面传参数
            _createOrEditModalItemDetail.open({ itemCategoryId: $('#SelectedItemCategoryID').text() });
        });

        //新增/编辑保存之后回到主页面，重新加载主页面代码组数据。
        abp.event.on('app.CreateOrEditModalSaved', function () {
            getObjects(true);
           
        });
        ////新增/编辑保存之后回到主页面，重新加载主页面代码数据
        abp.event.on('app.CreateOrEditModalSavedCode', function () {
            //alert($('#SelectedItemCategoryTitle').text());
            dataTableItemDetail.load($('#SelectedItemCategoryID').text(), $('#SelectedItemCategoryTitle').text().replace(":", ""));
        });
        dataTableItemDetail.init();
        //加载数据
        getObjects();
    });
})();